package trabajoISW2.data;

import java.util.List;

import javax.persistence.Query;

import trabajoISW2.domain.implementations.Order;

public class OrderDAO extends BaseDAO implements IOrderDAO {


	public OrderDAO(){
		
	}

	@Override
	public List<Order> getOrders() {

		Query q = getEm().createQuery("Select o FROM Order o");
		return q.getResultList();

	}

	@Override
	public Order getOrder(int id) {

		return getEm().find(Order.class, id);
	}

	@Override
	public Order createOrder(Order o) {
		getEm().getTransaction().begin();
		getEm().persist(o);
		getEm().getTransaction().commit();
		getEm().close();
		return o;
	}

	@Override
	public void updateOrder(Order o) {
		getEm().getTransaction().begin();
		getEm().merge(o);
		getEm().getTransaction().commit();
		getEm().close();

	}

	@Override
	public void deleteOrder(int id) {
		Order order = getEm().find(Order.class, id);
		if (order != null){
			getEm().getTransaction().begin();
			getEm().remove(order);
			getEm().getTransaction().commit();
			getEm().close();
		}

	}

}
